import RPi.GPIO as GPIO
import logging
from config.config import LIGHT_SENSOR_PIN

logger = logging.getLogger(__name__)

class LightSensor:
    def __init__(self):
        logger.info(f"初始化光敏电阻传感器，使用GPIO{LIGHT_SENSOR_PIN}")
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(LIGHT_SENSOR_PIN, GPIO.IN)
        
    def is_bright(self):
        """检测是否明亮
        返回：True表示明亮，False表示黑暗
        """
        status = not GPIO.input(LIGHT_SENSOR_PIN)
        logger.debug(f"光照状态：{'明亮' if status else '黑暗'}")
        return status
    
    def cleanup(self):
        """清理GPIO资源"""
        logger.info("清理光敏电阻传感器资源")
        GPIO.cleanup()